﻿using System.Collections.Generic;
using System.Text;

namespace CommonUtils.CodeFactory
{
    /// <summary>
    /// 方法模型
    /// </summary>
    public class Function
    {
        public string Define { get; private set; }

        public string Key { get; private set; }

        public bool IsStruct { get; private set; }

        public Function(string define, string key, bool isStruct)
        {
            Define = define;
            Key = key;
            IsStruct = isStruct;
        }

        private StringBuilder _codeComment = new StringBuilder();

        /// <summary>
        /// 获取注释，并记录插入点
        /// </summary>
        public int AddComment(List<string> lines)
        {
            while (lines.Count > 0)
            {
                var lastIndex = lines.Count - 1;
                var lastLine = lines[lastIndex];
                var trimLine = lastLine.Trim();
                if (string.IsNullOrEmpty(trimLine))
                {
                    lines.RemoveAt(lastIndex);
                }
                else if (trimLine.StartsWith("//") || trimLine.StartsWith("["))
                {
                    lines.RemoveAt(lastIndex);
                    _codeComment.Insert(0, lastLine + "\r\n");
                }
                else
                {
                    break;
                }
            }
            return lines.Count;
        }

        private List<string> _codeBody = new List<string>();

        private int _arrowCount;

        private int _semicolonCount;

        private int _leftBraceCount;

        private int _rightBraceCount;

        public bool CountKeyAndJudgeFinish(string line)
        {
            _codeBody.Add(line);

            line = line.Remove("\\\'", "\\\"");
            line = line.RemoveBetween("'", "'");
            line = line.RemoveBetween("\"", "\"");

            _arrowCount += line.CountOf("=>");
            _semicolonCount += line.CountOf(';');
            _leftBraceCount += line.CountOf('{');
            _rightBraceCount += line.CountOf('}');

            return (_arrowCount >= 1 && _semicolonCount >= 1) || ((_leftBraceCount > 0) && (_leftBraceCount == _rightBraceCount));
        }

        public string GetCode(FunctionOperation functionOperation = FunctionOperation.Default)
        {
            if (functionOperation == FunctionOperation.Simplify && _codeBody.Count == 4 && _codeBody[1].Trim() == "{" && _codeBody[3].Trim() == "}")
            {
                _codeBody[2] = _codeBody[2].Trim();
                if (_codeBody[2].StartsWith("return "))
                    _codeBody[2] = _codeBody[2].Remove(0, "return ".Length);
                _codeBody[2] = _codeBody[1].Replace("{", "=> ") + _codeBody[2];
                _codeBody.RemoveAt(1);
                _codeBody.RemoveAt(2);
            }

            if (functionOperation == FunctionOperation.LambdaFormat)
            {
                var space = _codeBody[0].GetStartSpace();
                if (_codeBody.Count == 2)
                    _codeBody[1] = space + _codeBody[1].Trim();
                if (_codeBody.Count == 1)
                    _codeBody[0] = _codeBody[0].Replace(" =>", "\r\n" + space + "=>");
            }

            return _codeComment + _codeBody.GetText();
        }
    }
}
